#ifndef _DSWCONSOLE_BASE_H
#define _DSWCONSOLE_BASE_H

#include "base.h"
#include <fcntl.h>
#include <termios.h>

typedef enum{
	CON_SESSION_KEY = 0,
	CON_SESSION_TEL = 1,
	CON_SESSION_RF = 2,
	CON_SESSION_GRPS = 3,
} EN_CON_SESSION_TYPE;

typedef struct{
	char nData[1024 * 5];
	char* pPos;
} ST_COMM_RECV_T, *LPST_COMM_RECV_T;

typedef enum{
	EN_LOCK_DISABLE,
	EN_LOCK_ENABLE,
	EN_MUT,
} EN_CONTROL_CODE;

typedef void (CB_BROADCAST_FUNC)(int nCmd, void* pParam, void* pCaller);
typedef struct{
	int nCmd;
	CB_BROADCAST_FUNC* pFunc;
} ST_BROADCAST_T, *LPST_BROADCAST_T;


class CDswconsole_base
{
public:
	CDswconsole_base(LPST_ENV_T pEnv, EN_CON_SESSION_TYPE nType, LPST_CONSOLE_INFO_T pInfo);
	virtual ~CDswconsole_base();
	bool IsValid(){return (m_nConsole != -1);};
private:
	virtual bool SeekToStartComm(LPST_COMM_RECV_T pCommRecv, int &nPayloadSize);
	virtual void ProcessData(char* pData, int nLen){};
protected:
	int MakeTrigDevId2AreId(unsigned int nTrigId);
	EN_CON_SESSION_TYPE GetSessionType(){return m_nType;};
	void ReOpen();
private:
	int OpenConsole(EN_CON_SESSION_TYPE nType);
	bool SetConsoleOpt(int nfd, int nspeed, int nbits, char nevent, int nstop);

	static void cb_OnRead(struct bufferevent *bev, void *ctx){((CDswconsole_base*)ctx)->OnRead(bev);};
	virtual void OnRead(struct bufferevent *bev);

protected:
	LPST_ENV_T m_pEnv;
	EN_CON_SESSION_TYPE m_nType;
	int m_nConsole;
	struct bufferevent *m_pBev;
	int m_nWantSize;
	bool m_bStartComm;
	ST_COMM_RECV_T m_nCommRecv;
	ST_CONSOLE_INFO_T m_nConsoleInfo;
};

#endif

